package com.jsc.controller;

import com.alibaba.fastjson.*;
import com.jsc.XiaMenBankSMUtil;
import com.jsc.common.utils.httputils.HttpSendRequest;
import com.jsc.pojo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.*;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;


@Slf4j
@Controller
@Scope(value = "prototype")
public class BankControllerAPI {


    /**
     * TODO 2.8投标人支付流水接口
     */
    @ResponseBody
    @RequestMapping("/bidderPaymentPipeline")
    public Map<String, ? extends Object> bidderPaymentPipeline(String i) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        System.out.println("↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓访问了 2.8投标人支付流水接口↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ");
        String uri = "";
        if (i.equals("1")) {
            uri = "https://xib02.test.xib.com.cn:48080/ifspeesi/gm/api/dlg003/DlgPaymentList";
        } else {
            uri = "http://127.0.0.1.vaiwan.com/xiamebank/bank/getBidderPaymentPipeline";
        }


        Map<String, String> senMap = new HashMap<>();
        senMap.put("pay_account_no", "8023100000000364");
        senMap.put("pay_account_name", "付款人名称");
        senMap.put("pay_trade_no", "");
        Map<String, Object> sendPacket = XiaMenBankSMUtil.getSendPacket(JSON.toJSONString(senMap), false);
        String nonceStr = (String) sendPacket.get("nonceStr");
        String timeStamp = (String) sendPacket.get("timeStamp");

        String sendMessage = JSON.toJSONString(sendPacket);
        System.out.println("发送加密的数据为：" + sendMessage);
        String sendResult = HttpSendRequest.doPost(uri, sendMessage);
        System.out.println("返回的数据为：" + sendResult);
        Map<String, String> isOK = XiaMenBankSMUtil.verifyAndDecrypt(sendResult, true, timeStamp, nonceStr);
        if (!isOK.get("code").equals("1")) {
            System.out.println("返回的数据解析失败");
            resultMap.put("status", "false");
            resultMap.put("message", isOK.get("msg"));
            return isOK;
        }
        String dataInfo = isOK.get("data");
        System.out.println("返回解密后数据为：" + dataInfo);
        resultMap.put("status", "true");
        resultMap.put("message", "流程操作成功");
        resultMap.put("data", dataInfo);
        System.out.println("↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑");

        return resultMap;
    }


    /**
     * TODO 2.7获取附件接口（通过URL）
     */
    @RequestMapping("/getAccessoryInterface")
    public void getAccessoryInterface() {
        System.out.println("↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓2.7获取附件接口↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ");

        String uri = "http://";
        Map<String, String> sendMap = new HashMap<>();
        sendMap.put("file_id", "file_id");
        String sendMapStr = JSON.toJSONString(sendMap);
        System.err.println("发送交易中心原文：" + sendMapStr);
        Map<String, Object> sendPacket = XiaMenBankSMUtil.getSendPacket(sendMapStr, false);
        String nonceStr = (String) sendPacket.get("nonceStr");
        String timeStamp = (String) sendPacket.get("timeStamp");
        String sendMessage = JSON.toJSONString(sendPacket);
        System.out.println("发送给交易中心加密为" + sendMessage);
        String sendResult = HttpSendRequest.doPost(uri, sendMessage);
        System.out.println("交易中心返回原文为：" + sendResult);
        Map<String, String> isOK = XiaMenBankSMUtil.verifyAndDecrypt(sendResult, true, timeStamp, nonceStr);
        String string = JSON.toJSONString(isOK);
        System.out.println("交易中心返回结果为:" + string);

        System.out.println("↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑");

    }

    /**
     * TODO 2.6电子保函查验
     */
    @ResponseBody
    @RequestMapping("/electronicGuaranteeCheck")
    public Map<String, ? extends Object> electronicGuaranteeCheck(String i) throws Exception {
        System.out.println("↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓2.6电子保函查验↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ");
        Map<String, String> resultMap = new HashMap<>();
        String uri = "";
        if (i.equals("1")) {
            uri = "https://xib02.test.xib.com.cn:48080/ifspeesi/gm/api/dlg003/DlgQuery";
        } else {
            uri = "http://127.0.0.1.vaiwan.com/xiamebank/bank/getElectronicGuaranteeCheck";
        }
        // 构造返回数据
        Map<String, String> senMap = new HashMap<>();
        senMap.put("guarantee_no", "DLG3002021053100002");
        senMap.put("serial_num", "1626829521532");
        String sentStr = JSON.toJSONString(senMap);
        System.out.println("原始报文为：" + sentStr);
        Map<String, Object> sendPacket = XiaMenBankSMUtil.getSendPacket(sentStr, false);
        String nonceStr = (String) sendPacket.get("nonceStr");
        String timeStamp = (String) sendPacket.get("timeStamp");
        String sendMessage = JSON.toJSONString(sendPacket);
        String sendResult = HttpSendRequest.doPost(uri, sendMessage);
        System.out.println("保函查验返回的数据为：" + sendResult);
        Map<String, String> isOK = XiaMenBankSMUtil.verifyAndDecrypt(sendResult, true, timeStamp, nonceStr);
        if (!isOK.get("code").equals("1")) {
            System.out.println("返回的数据解析失败");
            resultMap.put("status", "false");
            resultMap.put("message", isOK.get("msg"));
            return isOK;
        }
        String dataInfo = isOK.get("data");
        System.out.println("返回解密后数据为：" + dataInfo);
        resultMap.put("status", "true");
        resultMap.put("message", "流程操作成功");
        resultMap.put("data", dataInfo);
        System.out.println("↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑");

        return resultMap;


    }


    /**
     * TODO 2.5电子保函验真  OK
     *
     * @return
     * @throws Exception
     */
    // http://127.0.0.1.vaiwan.com/xiamebank/bhVerification
    @ResponseBody
    @RequestMapping("/bhVerification")
    public Map bhVerification(String i) throws Exception {
        System.err.println("-------------------------走了保函验真接口-------------------------");
        Map<String, Object> resultMap = new HashMap<>();
        String uri = "";
        if (i.equals("1")) {
            uri = "https://xib02.test.xib.com.cn:48080/ifspeesi/dlg/gm/api/dlg003/DlgVerification";
        } else {
            uri = "http://127.0.0.1.vaiwan.com/xiamebank/bank/getMEssag";
        }
        System.err.println("uri 为" + uri);
        HashMap<String, String> sendMess = new HashMap<>();
        sendMess.put("serial_num", "1626829521532");
        Map<String, Object> sendPacket = XiaMenBankSMUtil.getSendPacket(JSON.toJSONString(sendMess), false);
        String nonceStr = (String) sendPacket.get("nonceStr");
        String timeStamp = (String) sendPacket.get("timeStamp");
        // 构建发送信息
        String sendMessage = JSON.toJSONString(sendPacket);
        System.out.println("要发送的信息为：" + sendMessage);
        String filePath = "D:\\Project\\xiamenguojiyinhang\\sendfile\\TBaoHanptfd2021062601.ofd";
        Map<String, String> map = HttpSendRequest.sendMultiFormMixData(uri, filePath, sendMessage);
        if (!map.get("sendStatus").equals("200")) {
            System.out.println("发送失败: " + JSON.toJSONString(map));
            return map;
        }
        // 发送成功 获取数据
        String mess = map.get("sendMess");
        System.out.println("发送成功返回原文数据为：" + mess);
        // 数据验证
        Map<String, String> isOk = XiaMenBankSMUtil.verifyAndDecrypt(mess, true, timeStamp, nonceStr);
        if (!isOk.get("code").equals("1")) {
            System.out.println("数据解密失败" + JSON.toJSONString(isOk));
            return isOk;
        }
        String data = isOk.get("data");
        System.out.println("数据验证成功，解密为: " + data);
        Map returnMap = JSON.parseObject(data, Map.class);
        String status = (String) returnMap.get("status");

        if (!status.equals("1")) {
            resultMap.put("status", "false");
            resultMap.put("message", "状态为" + status + "流程结束");
            resultMap.put("data", data);
            return resultMap;
        }
        resultMap.put("status", "true");
        resultMap.put("message", "状态为" + status + "流程结束");
        resultMap.put("data", data);
        System.out.println("↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑");

        return resultMap;
    }


//    http://127.0.0.1.vaiwan.com/xiamebank/getGuaranteeInfo


    /**
     * 合作方接收到电子保函信息，处理 返回结果
     * TODO 2.4  OK
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getGuaranteeInfo")
    @ResponseBody
    public Map getGuaranteeInfo(HttpServletRequest request) throws IOException {
        System.out.println("↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓2.4推送电子保函文件↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ");

        request.setCharacterEncoding("UTF-8");
        Map<String, Object> map = new HashMap<>();
        String data = request.getParameter("data");
        String timeStamp = request.getParameter("timeStamp");
        String nonceStr = request.getParameter("nonceStr");
        String sign = request.getParameter("sign");
        String encrypt = request.getParameter("encrypt");

        System.out.println("--------------------------接收方数据----------------------");
        System.out.println("data 数据为：" + data);
        System.out.println("timeStamp 数据为：" + timeStamp);
        System.out.println("nonceStr 数据为：" + nonceStr);
        System.out.println("sign 数据为：" + sign);
        System.out.println("encrypt 数据为：" + encrypt);
        System.out.println("----------------------------------------------------------");

        Map<String, String> getMap = new HashMap<>();
        getMap.put("data", data);
        getMap.put("timeStamp", timeStamp);
        getMap.put("nonceStr", nonceStr);
        getMap.put("sign", sign);
        getMap.put("encrypt", encrypt);
        String getStr = JSON.toJSONString(getMap);
        log.info("返回的时间戳为: {}，随机数为: {}", timeStamp, nonceStr);
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        multipartRequest.setCharacterEncoding("UTF-8");
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        // 校验报文是否超时等
        Map<String, String> verifyAndDecryptResult = XiaMenBankSMUtil.verifyAndDecrypt(getStr, true);
        if ("0".equals(verifyAndDecryptResult.get("code"))) {
            System.out.println("检验报文是否正确： = " + JSONObject.toJSONString(verifyAndDecryptResult));
            return verifyAndDecryptResult;
        }
        String data1 = verifyAndDecryptResult.get("data");

        System.out.println("银行返回的报文解析后为：" + data1);


        String path = "D:\\Project\\xiamenguojiyinhang\\files";
        log.info("getGuaranteeInfo验证和解密结果成功,准备下载保函文件");
        MultipartFile guarantMultipartFile = fileMap.get("guarantee_file");

        if (guarantMultipartFile.getSize() <= 0) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("status", "1");
            HashMap<Object, Object> serialmap = new HashMap<>();
            serialmap.put("mess", "文件下载失败");
            resultMap.put("data", serialmap);
            /*返回 */
            Map<String, Object> sendPacket = XiaMenBankSMUtil.getSendPacket(JSON.toJSONString(resultMap), true, timeStamp, nonceStr);
            String string = JSON.toJSONString(sendPacket);
            System.out.println("文件下载失败：" + string);
            return sendPacket;
        }

        String timetemp = System.currentTimeMillis() + "_";
        String originalFilename = guarantMultipartFile.getOriginalFilename();
        System.out.println("文件名为：" + originalFilename);
        String fileNewNAme = "OOOFFFDD" + originalFilename.substring(originalFilename.lastIndexOf("."));
        String guaranteeDocument = path + "\\" + timetemp + fileNewNAme;
        File guarantFile = new File(guaranteeDocument);
        if (!guarantFile.exists()) {
            guarantFile.createNewFile();
        }
        // 下载文件
        try {
            InputStream inputStream = guarantMultipartFile.getInputStream();
            writeToLocal(guaranteeDocument, inputStream);
            inputStream.close();
        } catch (IOException e) {
            guarantFile.delete();
            map.put("status", "1");
            Map<String, Object> sendMap = XiaMenBankSMUtil.getSendPacket(JSONObject.toJSONString(map), true, timeStamp, nonceStr);
            log.info("文件下载失败" + e.getMessage());
            return sendMap;
        }
        System.out.println("保存文件成功");
        log.info("getGuaranteeInfo解密报文为：" + verifyAndDecryptResult.get("msg"));
        // 处理数据......
        /**
         * 处理生成 返回结果。。。。
         */

        /*返回 */
        Map<String, Object> dataMap = new HashMap<>();
        Map<String, String> resultBankMap = new HashMap<>();
        resultBankMap.put("serial_num", String.valueOf(System.currentTimeMillis()));
        dataMap.put("data", resultBankMap);
        dataMap.put("status", "1");
        String returnMsg = JSON.toJSONString(dataMap);
        System.err.println("返回银行的原文为：" + returnMsg);
        Map<String, Object> sendPacket = XiaMenBankSMUtil.getSendPacket(returnMsg, false, timeStamp, nonceStr);
        String string = JSON.toJSONString(sendPacket);
        System.out.println("我返回银行的报文为：" + string);
        System.out.println("↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑");

        return sendPacket;
    }

    public static void main(String[] args) {
        String cc = "/pt";
        System.out.println(cc.substring(1));



/*        String json = "{\"data\":{\"payment_info_list\":[{\"pay_account_name\":\"8023100000000364\",\"pay_account_no\":\"8023100000000364\",\"pay_amount\":390.00,\"pay_time\":\"202101221421\",\"pay_trade_no\":\"3dfecdd9-8f51-4905-a690-70c8e293584a\",\"note\":\"测试付款附言\"},{\"pay_account_name\":\"dddd\",\"pay_account_no\":\"dddd\",\"pay_amount\":390.00,\"pay_time\":\"dddd\",\"pay_trade_no\":\"3dfecdd9-8f51-4905-a690-70c8e293584a\",\"note\":\"dddd\"},{\"pay_account_name\":\"aaa\",\"pay_account_no\":\"aaa\",\"pay_amount\":390.00,\"pay_time\":\"aaa\",\"pay_trade_no\":\"aaa-8f51-4905-aaa-70c8e293584a\",\"note\":\"aaa\"}]},\"status\":\"1\"}";
        Map map = JSON.parseObject(json, Map.class);
        Map data = JSON.parseObject(map.get("data").toString(), Map.class);
        Object payment_info_list = data.get("payment_info_list");
        for (Object o : JSON.parseArray(payment_info_list.toString())) {
            System.out.println("数据为：" + o);
        }*/
    }

//ackMsg status 1 成功  0 失败

    /**
     * TODO 2.3推送保函申请订单处理结果 OK
     * 银行返回保函申请订单处理结果。
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/dataHandler")
    @ResponseBody
    public Map<String, Object> dataHandler(HttpServletRequest request) {
        System.out.println("↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ 2.3推送保函申请订单处理结果↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ");
        String timeStamp = "";
        String nonceStr = "";
        try {
            request.setCharacterEncoding("UTF-8");
            StringBuffer data = new StringBuffer();
            String line = null;
            BufferedReader reader = null;

            reader = request.getReader();
            while (null != (line = reader.readLine())) {
                data.append(line);
            }

            String publicMessage = data.toString();
            log.info("报文为:{}" + publicMessage);

            Map bankMap = JSON.parseObject(publicMessage, Map.class);
            timeStamp = (String) bankMap.get("timeStamp");
            nonceStr = (String) bankMap.get("nonceStr");
            log.info("银行返回的时间戳为: {}，随机数为: {}", timeStamp, nonceStr);
            // 校验报文是否超时等
            Map<String, String> verifyAndDecryptResult = XiaMenBankSMUtil.verifyAndDecrypt(publicMessage, true);
            log.info("dataHandler报文：" + JSONObject.toJSONString(verifyAndDecryptResult));
            if ("0".equals(verifyAndDecryptResult.get("code"))) {
                System.out.println("verifyAndDecryptResult = " + JSONObject.toJSONString(verifyAndDecryptResult));
                Map<String, Object> map = new HashMap<>();
                map.put("status", "0");
                map.put("ackMsg",verifyAndDecryptResult.get("msg"));
                Map<String, Object> sendPacket = XiaMenBankSMUtil.getSendPacket(JSON.toJSONString(map), false, timeStamp, nonceStr);
                return sendPacket;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("dataHandler:{}", e);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("status", "1");
        Map<String, Object> sendPacket = XiaMenBankSMUtil.getSendPacket(JSON.toJSONString(map), false, timeStamp, nonceStr);
        log.info("返回银行加密报文为：{}" + sendPacket);
        System.out.println("↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑");
        return sendPacket;

    }


//    @RequestMapping(value = "/dataHandler1")
//    @ResponseBody
//    public Map<String, Object> dataHandler1(HttpServletRequest request) throws IOException {
//        System.out.println("↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ 2.3推送保函申请订单处理结果↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ");
//        Map<String, Object> map = new HashMap<>();
//        request.setCharacterEncoding("UTF-8");
//        StringBuffer data = new StringBuffer();
//        String line = null;
//        BufferedReader reader = null;
//        reader = request.getReader();
//        while (null != (line = reader.readLine())) {
//            data.append(line);
//        }
//        String publicMessage = data.toString();
//        log.info("报文为:{}" + publicMessage);
//        Map bankMap = JSON.parseObject(publicMessage, Map.class);
//        String timeStamp = (String) bankMap.get("timeStamp");
//        String nonceStr = (String) bankMap.get("nonceStr");
//        log.info("银行返回的时间戳为: {}，随机数为: {}", timeStamp, nonceStr);
//
//        //检验银行发给的参数是否正确
//        Map<String, String> verifyAndDecryptResult = SMUtilTest.verifyAndDecrypt(publicMessage, true);
//        log.info("dataHandler报文：" + JSONObject.toJSONString(verifyAndDecryptResult));
//        if ("0".equals(verifyAndDecryptResult.get("code"))) {
//            System.out.println("verifyAndDecryptResult = " + JSONObject.toJSONString(verifyAndDecryptResult));
//        map.put("status","0");
//        map.put("ackMsg",verifyAndDecryptResult.get("msg"));
//        }
//
//        return map;
//    }

    /**
     * 2。2推送
     *
     * @throws
     */
    @RequestMapping(value = "/orderStatusChange")
    @ResponseBody
    public void orderStatusChange() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("order_no", "BH000004");
        map.put("block_number", "E3504010401100120001001");
        map.put("block_name", "标段名称123");
        map.put("principal", "福建省远胜市政园林有限公司");
        map.put("creditor", "三明市城市建设投资集团有限公司");
        map.put("applicant", "三明市国有融资担保有限公司");
        map.put("order_status_old", "WAIT_APPROVAL_DATA");
        map.put("order_status", "CANCEL");
        Map<String, Object> psJson = XiaMenBankSMUtil.getSendPacket(JSONObject.toJSONString(map), false);
        String url = "https://xib02.test.xib.com.cn:48080/ifspeesi/gm/api/dlg003/DlgOrderStUpdate";
        String resultBank = HttpSendRequest.doPost(url, JSONObject.toJSONString(psJson));
        Map<String, String> resultMap = XiaMenBankSMUtil.verifyAndDecrypt(resultBank, true);
        System.out.println(JSONObject.toJSONString(resultMap));
    }


    /**
     * 2.1推送  ok
     *
     * @throws
     */
    @RequestMapping(value = "/putBankOrderInfo")
    @ResponseBody
    public void pushBhInfo() {
        OrderInfoEntity oie = new OrderInfoEntity();
        String order = "BH202107";
        String num = "22001";
        oie.setCenter_name("福建省公共资源交易中心");
        oie.setOrder_no(order+num);
        oie.setOrder_status("WAIT_APPROVAL_DATA"); // 待审批
        oie.setProject_approve_number("pzwh_"+num);
        oie.setProject_union_code("GS347982738942_"+num);
        oie.setProject_number("ZBXMBH"+num);
        oie.setProject_name("招标项目名称_"+num);
        oie.setBlock_name("标段名称_"+num);
        oie.setBlock_number("标段编号_"+num);
        oie.setProject_type("");
        oie.setBid_project_category("A01");
        oie.setAddress("");
        oie.setProject_city_code("项目行政区代码_"+num);

//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
//        Date openTime = sdf1.parse("2021-08-09");
//        String openTimeStr = sdf.format(openTime);
//        String enddate =
//                CommUtil.plusDay(
//                        new BigDecimal(insureInfo.getVALID_PERIOD()).toBigInteger().intValue(),
//                        "yyyyMMddHHmmss",
//                        insureInfo.getBID_OPEN_TIME());
//
//        Date endTime = sdf1.parse(enddate);
//        String endTimeStr = sdf.format(endTime);

        oie.setBid_deadline("2021-08-09");
        oie.setValid_day("30");
        oie.setProject_amount("200.00");//-------------->待确定
        oie.setContract_reckon_amount("3000000.00");
        oie.setGuarantee_amount("3000000.00");
        oie.setGuarantee_end_date("2021-09-09");//=======>需要修改格式
        oie.setCreditor("招标人_"+num);
        oie.setCreditor_scc("GB2312UTF8GBK1024");
        oie.setCreditor_name("招标人联系人_"+num);
        oie.setCreditor_phone("136033"+num);
        oie.setCreditor_account_bank_no("招标人银行账户"+num);
        oie.setCreditor_account_bank_name("招标人银行开户行"+num);
        oie.setCreditor_bank_account_name("招标人银行账户名称_"+num);
        oie.setPrincipal("被保证人名称_"+num);
        oie.setPrincipal_scc("BBZRTYSHXYDM"+num);
        oie.setPrincipal_address("被保证人地址_"+num);
        oie.setPrincipal_email(order+num+"@gmail.com");
        oie.setPrincipal_name("被保证人联系人"+num);
        oie.setPrincipal_cert_type("01");//01
        oie.setPrincipal_id_card("130185199811248904");
        oie.setPrincipal_phone("136033"+num);
        oie.setPrincipal_legal_person("被保证人法定代表人"+num);
        oie.setPrincipal_legal_cert_type("01");
        oie.setPrincipal_legal_id_card("130185199811248905");
        oie.setPrincipal_legal_phone("136033+"+num);
        oie.setPrincipal_qualf_name("建筑装饰装修工程专业承包");
        oie.setPrincipal_qualf_grade("二级");
        oie.setGuarantor("担保公司名称"+num);
        oie.setApply_company_social_credit_code("担保公司统一社会信用代码"+num);
        oie.setTender_notice_url("http://101.201.127.167:8189/pt/uApi/downFile?orderid=aU6epCu%2Bvid6Ed1Qbj7lRA%3D%3D&fileType=nsZCHZ02XaABTmNoTXW8aw%3D%3D");
        oie.setTender_file_url("http://101.201.127.167:8189/pt/uApi/downFile?orderid=aU6epCu%2Bvid6Ed1Qbj7lRA%3D%3D&fileType=nsZCHZ02XaABTmNoTXW8aw%3D%3D");
        oie.setGuarantee_template_file_id("http://101.201.127.167:8189/pt/uApi/downFile?orderid=aU6epCu%2Bvid6Ed1Qbj7lRA%3D%3D&fileType=nsZCHZ02XaABTmNoTXW8aw%3D%3D");//----------->模板id
        oie.setGuarantee_template_file_name("施工保函");//---------->模板名称
        oie.setSeller_open_account("181040100100161305");
        oie.setSeller_open_bank("三明市兴业银行列东支行");
        oie.setSeller_address("福建省三明市梅列区东街1号十五层");
        oie.setSeller_phome("0598-5176007");
        oie.setHandler("雷虹");
        oie.setProduct_name("保函服务费");
        oie.setInvoice_amount("20.00");
        oie.setAmount_of_chinese("贰拾元整");
        oie.setInvoice_remark("");
        oie.setCustomer_open_account("13050161583200000316");
        oie.setCustomer_open_bank("测试银行");
        oie.setCustomer_address("福建省三明市梅列区乾龙新村416幢12层2号");
        oie.setCustomer_phone("0311-89281515");
        List<Map<String, String>> fileList = new ArrayList<>();
        Map<String, String> fileMap1 = new HashMap<>();
        fileMap1.put("fileNm", "营业执照");
        fileMap1.put("fileTp", "1");
        fileMap1.put("fileUrl", "http://101.201.127.167:8189/pt/uApi/downFile?orderid=aU6epCu%2Bvid6Ed1Qbj7lRA%3D%3D&fileType=nsZCHZ02XaABTmNoTXW8aw%3D%3D");
        fileMap1.put("md5Value", "");
        Map<String, String> fileMap2 = new HashMap<>();
        fileMap2.put("fileNm", "法人身份证明");
        fileMap2.put("fileTp", "2");
        fileMap2.put("fileUrl", "http://101.201.127.167:8189/pt/uApi/downFile?orderid=aU6epCu%2Bvid6Ed1Qbj7lRA%3D%3D&fileType=nsZCHZ02XaABTmNoTXW8aw%3D%3D");
        fileMap2.put("md5Value", "");
        Map<String, String> fileMap3 = new HashMap<>();
        fileMap3.put("fileNm", "招标文件");
        fileMap3.put("fileTp", "3");
        fileMap3.put("fileUrl", "http://101.201.127.167:8189/pt/uApi/downFile?orderid=aU6epCu%2Bvid6Ed1Qbj7lRA%3D%3D&fileType=wbwv7KjsMJ5OsKHHsGWKQA%3D%3D");
        fileMap3.put("md5Value", "");
        Map<String, String> fileMap4 = new HashMap<>();
        fileMap4.put("fileNm", "电子保单（合同）原文件");
        fileMap4.put("fileTp", "4");
        fileMap4.put("fileUrl", "http://101.201.127.167:8189/pt/uApi/downFile?orderid=aU6epCu%2Bvid6Ed1Qbj7lRA%3D%3D&fileType=wbwv7KjsMJ5OsKHHsGWKQA%3D%3D");
        fileMap4.put("md5Value", "");
        Map<String, String> fileMap5 = new HashMap<>();
        fileMap5.put("fileNm", "电子保函（合同）PDF文件");
        fileMap5.put("fileTp", "5");
        fileMap5.put("fileUrl", "http://101.201.127.167:8189/pt/uApi/downFile?orderid=aU6epCu%2Bvid6Ed1Qbj7lRA%3D%3D&fileType=xpjum6SHgsKgLID7U7em1A%3D%3D");
        fileMap5.put("md5Value", "");
        Map<String, String> fileMap6 = new HashMap<>();
        fileMap6.put("fileNm", "电子保函申请单原文件");
        fileMap6.put("fileTp", "6");
        fileMap6.put("fileUrl", "http://101.201.127.167:8189/pt/uApi/downFile?orderid=aU6epCu%2Bvid6Ed1Qbj7lRA%3D%3D&fileType=UQYf7lCNj5wObtMOn6lNBA%3D%3D");
        fileMap6.put("md5Value", "");
        Map<String, String> fileMap7 = new HashMap<>();
        fileMap7.put("fileNm", "电子保函申请单PDF文件");
        fileMap7.put("fileTp", "7");
        fileMap7.put("fileUrl", "http://101.201.127.167:8189/pt/uApi/downFile?orderid=aU6epCu%2Bvid6Ed1Qbj7lRA%3D%3D&fileType=xpjum6SHgsKgLID7U7em1A%3D%3D");
        fileMap7.put("md5Value", "");
        Map<String, String> fileMap8 = new HashMap<>();
        fileMap8.put("fileNm", "出具保函承诺书");
        fileMap8.put("fileTp", "8");
        fileMap8.put("fileUrl", "http://101.201.127.167:8189/pt/uApi/downFile?orderid=aU6epCu%2Bvid6Ed1Qbj7lRA%3D%3D&fileType=xpjum6SHgsKgLID7U7em1A%3D%3D");
        fileMap8.put("md5Value", "");
        fileList.add(fileMap1);
        fileList.add(fileMap2);
        fileList.add(fileMap3);
        fileList.add(fileMap4);
        fileList.add(fileMap5);
        fileList.add(fileMap6);
        fileList.add(fileMap7);
        fileList.add(fileMap8);
        oie.setFileList(JSONObject.toJSONString(fileList));
        String string = JSONObject.toJSONString(oie);
        System.out.println("发送的原始数据为：" + string);
        Map psJson = XiaMenBankSMUtil.getSendPacket(JSONObject.toJSONString(oie), false);
        String url = "https://xib02.test.xib.com.cn:48080/ifspeesi/gm/api/dlg003/DlgOrderApply";
        String resultBank = HttpSendRequest.doPost(url, JSONObject.toJSONString(psJson));
        System.out.println("银行返回原文为：" + resultBank);
        Map<String, String> map = XiaMenBankSMUtil.verifyAndDecrypt(resultBank, true);
        System.out.println("返回结果：" + JSONObject.toJSONString(map));

    }


    public void writeToLocal(String destination, InputStream input)
            throws IOException {
        int index;
        byte[] bytes = new byte[1024];
        FileOutputStream downloadFile = new FileOutputStream(destination);
        while ((index = input.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }
        input.close();
        downloadFile.close();

    }


}
